<style rel="stylesheet/scss" lang="scss" scoped>
</style>

<template>
    <div class="container">
        <el-card shadow="hover" style="padding:0">
            <!-- 操作按钮 -->
            <el-card class="operate_container" shadow="never">
                <div class="wrapper">
                    <div><i class="el-icon-tickets" style="margin-right: 10px;"></i>数据列表</div>
                    <div>
                        <el-button type="primary" icon="el-icon-plus" size="medium" circle @click="addOrUpdateVisible"></el-button>
                        <el-button type="danger" icon="el-icon-delete" size="medium" circle @click="handleDelete"></el-button>
                        <el-button type="primary" icon="el-icon-refresh" size="medium" circle @click="handleResetSearch('searchForm')"></el-button>
                        <el-button type="primary" icon="el-icon-search" size="medium" circle @click="searchVisible = !searchVisible"></el-button>
                    </div>
                </div>
            </el-card>

            <!-- 条件过滤 -->
            <el-card class="filter_container" shadow="never" v-show="searchVisible">
                <div style="margin-top: 15px">
                    <el-form ref="searchForm" :inline="true" :model="searchForm" size="medium" label-width="120px">
                        <el-form-item label="名称" prop="name">
	                        <el-input v-model="searchForm.name" clearable placeholder="名称"></el-input>
	                    </el-form-item>
                         <el-form-item label=" ">
                            <el-button type="primary" @click="handleSearch()" size="medium">搜索</el-button>
                            <el-button @click="handleResetSearch('searchForm')" size="medium">重置</el-button>
	                    </el-form-item>
                    </el-form>
                </div>
            </el-card>

            <!-- 表格 -->
            <el-table
                ref="table" size="small" empty-text="暂无数据"
                element-loading-text="给我一点时间"
                fit stripe border highlight-current-row
                v-loading="dataListLoading"
                :data="dataList"
                row-key="id"
                :default-expand-all="true"
                :header-cell-style="{'padding-top': '10px', 'height': '50px', 'color': '#515A6E', 'background-color': '#f8f8f9'}"
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                @selection-change="handleSelection">
                <el-table-column label="名称" prop="name"/>
                <el-table-column label="URL" prop="url"/>
                <el-table-column label="排序" prop="sort" align="center"/>
                <el-table-column label="状态" prop="status" align="center"/>
                <el-table-column label="创建时间" prop="ctime" align="center"/>
                
                <el-table-column label="操作" align="center" fixed="right">
                    <template slot-scope="scope">
                        <div class="table_action">
                            <el-button size="small" @click="navTo(scope.row)">权限标识</el-button>
                            <el-button type="primary" icon="el-icon-edit" circle size="small" @click="addOrUpdateVisible(scope.row)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle size="small" @click="handleDelete(scope.row)"></el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 窗口 -->
            <el-dialog
                :title="!form.id ? '新增' : '修改'"
                :visible.sync="visible" :close-on-click-modal="false" @close="handleDialogClose">
                <el-form ref="form" :model="form" :rules="rules" label-width="120px">

                    <el-form-item label="父级" prop="pid">
                        <el-cascader
                            clearable
                            v-model="form.pid"
                            :options="options"
                            :props="defaultProps"
                            placeholder="请选择菜单"
                            style="width: 80%"
                        ></el-cascader>
                    </el-form-item>
                	
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="form.name" placeholder="名称" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="URL" prop="url">
                        <el-input v-model="form.url" placeholder="URL" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="排序" prop="sort">
                        <el-input v-model="form.sort" placeholder="排序" style="width: 80%;"></el-input>
                    </el-form-item>
                    
                </el-form>

                <div slot="footer" class="dialog-footer">
                    <el-button @click="visible = false">取 消</el-button>
                    <el-button @click="handleSubmit('form')" type="primary" :loading="submitLoading">提 交</el-button>
                </div>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
import { save, deleted, updated, info, list, page, selectMyPage, listWithTree } from "@/api/admin/sysmenu"

export default {
    data() {
        return {
            visible: false,
            searchVisible: false,
            submitLoading: false,

            dataList: [],
            totalPage: 0,
            dataListLoading: true,

            options: [],
            defaultProps: {
                label: "name",
                value: "id",
                children: "children",
                checkStrictly: true,
                emitPath: false,
                expandTrigger: 'hover'
            },

            searchForm: {
                sortColumns: null,
                sortType: null,
                name: null
            },
            form: {
                id: null,
                pid: null,
                name: null,
                url: null,
                sort: null,
                status: null,
                ctime: null,
                utime: null
            },
            rules: {
                name: [
                    { required: true, message: '名称不能为空', trigger: 'blur' }
                ],
                url: [
                    { required: true, message: 'URL不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    mounted() {
    	this.$nextTick(function () {
	        this.getDataList()
    	})
    },
    methods: {
        handleSelection(selection) { // 多选
            this.selectionRows = selection
        },
        handleSizeChange(val) { // 每页数量
            this.searchForm.limit = val
            this.getDataList()
        },
        handleCurrentChange(val) { // 页码
            this.searchForm.page = val
            this.getDataList()
        },
        handleResetSearch(name) { // 重置搜索表单
            this.handleReset(name)
            this.getDataList()
        },
        handleReset(name) { // 重置表单
            const refs = this.$refs
            refs[name].resetFields()
        },
        handleSearch() { // 单条件搜索
            this.getDataList()
        },
        handleDialogClose() { //表单窗口关闭
            this.handleReset('form')
        },

        navTo(row) {
            if(row.url) {
                this.$router.push({ name: 'initPermission', query: { id: row.id, url: row.url } })
            } else {
                this.$router.push({ name: 'initPermission', query: { id: row.id } })
            }
        },

		/**
         * 打开表单窗口
         */
		addOrUpdateVisible(row) {
            this.visible = true
            if (row.id) {
                this.getInfo(row.id).then(res => {
                    this.form = Object.assign({}, res.data)
                })
            } else {
                this.form.id = null
            }
        },

		/**
         * 提交表单
         */
        handleSubmit(name) {
        	const refs = this.$refs
            refs[name].validate((valid) => {
                if (valid) {
                    this.submitLoading = true
                    if (!this.form.id) {
                        save(this.form).then(res => {
                            if (this.ResultMessage(res)) {
                                this.submitLoading = false
                                this.visible = false
                                this.handleReset('form')
                                this.getDataList()
                            } else {
                                this.submitLoading = false
                            }
                        }).catch(error => {
                            this.submitLoading = false
                            this.$notify({ title: '错误', message: error, type: 'error' });
                        })
                    } else if (this.form.id) {
                        updated(this.form).then(res => {
                            if (this.ResultMessage(res)) {
                                this.submitLoading = false
                                this.visible = false
                                this.handleReset('form')
                                this.getDataList()
                            } else {
                                this.submitLoading = false
                            }
                        }).catch(error => {
                            this.submitLoading = false
                            this.$notify({ title: '错误', message: error, type: 'error' });
                        })
                    } else {
                        console.log('未知操作');
                    }
                }
            })
        },
		
		/**
         * 删除
         */
        handleDelete(row) { 
            let ids = []
            if (row.id != undefined) {
                ids.push(row.id)
            }
            if(this.selectionRows) {
                this.selectionRows.filter((item, index) => {
                    ids.push(item.id)
                })
            }
            if (ids.length <= 0) {
                this.$notify({ title: '提示', message: '请选择需要删除的记录！', type: 'info' });
            } else {    
                this.$confirm('确定要删除？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
                    deleted({ idList: ids }).then(res => {
                        if (this.ResultMessage(res)) {
                            this.getDataList()
                        } 
                    }).catch(error => {
                        this.$notify({ title: '错误', message: error, type: 'error' });
                    })
                }).catch(() => {})
            }
        },

		/**
         * 详细信息
         */
        getInfo(id) {
            if (id !== undefined) {
                return info(id).then(res => {
                    if (this.ResultMessage(res, false)) {
                        return res
                    } else {
                        this.$notify({ title: '错误', message: error, type: 'error' });
                    }
                }).catch(error => {
                    this.$notify({ title: '错误', message: error, type: 'error' });
                })
            }
        },
		
        /**
         * 数据列表
         */
        getDataList() {
            listWithTree(this.searchForm).then(res => {
                if (this.ResultMessage(res, false)) {
                    this.dataList = res.data
                    this.options = res.data
                    this.dataListLoading = false
                } else {
                    this.dataListLoading = false
                }
            }).catch(error => {
                this.dataListLoading = false
                this.$notify({ title: '错误', message: error, type: 'error' });
            })
        }
    }
}
</script>